<?php
include_once 'MySQLDAO.php';

/**
 * Definition of Class Facilities
 * This class represents a data access object for the database table 'facilities'.
 */
class Facilities {


	private $dao = null;

	private $i = 1;

	/**
	 * associated database field: facilityId, type: int, is nullable: false
	 */
	private $facilityId  = 0;

	/**
	 * associated database field: facilityDescription, type: varchar, is nullable: true, character set: utf8, max length: 45
	 */
	private $facilityDescription;



	/**
	 * Default constructor
	 * @param value some value
	 */
	function __construct(){
		$this->dao = new MySQLDao("Facilities");
	}


	/**
	 * Initialize the business object with data read from the DB.
	 * @param row database row array containing the values from one read record
	 */
	public function init($row){
		if(isset($row['facilityId'])) $this->facilityId = $row['facilityId'];
		if(isset($row['facilityDescription'])) $this->facilityDescription = $row['facilityDescription'];
	}


	/**
	 * Returns the string representation for this object
	 * @param escapeQuotationMarks boolean value to indicate if quotation marks in value strings should be escaped, default value is true
	 * @return String String representation of Facilities
	 */
	public function toString($escapeQuotationMarks=true){
		$s = '[';
		$s .= 'facilityId: '.(($escapeQuotationMarks===true) ? addslashes($this->facilityId) : $this->facilityId).', ';
		$s .= 'facilityDescription: '.(($escapeQuotationMarks===true) ? addslashes($this->facilityDescription) : $this->facilityDescription);
		$s .= ']';
		return $s;
	}


	/**
	 * Returns the JSON (JavaScript Object Notation) string representation for this object
	 * @return String JSON representation of Facilities
	 */
	public function toJSONString(){
		$fieldValues = array ("facilityId"=>$this->facilityId, "facilityDescription"=>$this->facilityDescription);
		return json_encode($fieldValues);
	}


	/**
	 * Returns a HTML representation of this object using DIVs for formatting via css
	 * @param escapeHTML boolean value to indicate if value strings should be html escaped, default value is true
	 * @return String the HTML representation of the Facilities object
	 */
	public function toHTMLString($escapeHTML=true){
		$htmlCode = "<div class=\"facilities\">\n";
		$htmlCode .= '	<div class="facilities_facilityId">'.(($escapeHTML===true) ? htmlspecialchars($this->facilityId) : $this->facilityId)."</div>\n";
		$htmlCode .= '	<div class="facilities_facilityDescription">'.(($escapeHTML===true) ? htmlspecialchars($this->facilityDescription) : $this->facilityDescription)."</div>\n";
		$htmlCode .= "</div>\n";
		return $htmlCode;
	}
	
	public function getData($where) {
		$rows = $this->dao->getData($where);
		return $rows;
	}


	public static function loadAll(){
		$facilities = new Facilities();
		$rows = $facilities->dao->getData("");
		$facilitiess = array();
		for ($index = 0; $index < sizeof($rows); $index++) {
			$d = new Facilities();
			$d->init($rows[$index]);
			array_push($facilitiess,$d);
		}
		return $facilitiess;
	}


	/**
	 * Load the Facilities uniquely by its primary key.
	 * @param facilityId the primary key
	 * @return Facilities Instance of {@link Facilities}
	 */
	public static function loadUnique($facilityId){
		$facilities = new Facilities();
		$rows = $facilities->dao->getData("`facilityId`='$facilityId'");
		$facilities->init($rows[0]);
		return $facilities;
	}


	/**
	 * Insert this object into the DB
	 * @return number new id (auto increment value) genereated
	 */
	public function insert(){
		$list = array("facilityId"=>$this->facilityId, "facilityDescription"=>$this->facilityDescription);
		return $this->dao->insertRecord($list);
	}


	/**
	 * Update this object into the DB
	 * @return number the number of updated records
	 */
	public function update(){
		$list = array("facilityId"=>$this->facilityId, "facilityDescription"=>$this->facilityDescription);
		$where = "`facilityId`='$facilityId'";
		return $this->dao->updateRecord($list,$where);
	}


	/**
	 * Initialize the business object with data read from the DB.
	 */
	public function initPOST(){
		if (isset($_POST['facilityId'])){
			$this->facilityId = $_POST['facilityId'];
		}
		if (isset($_POST['facilityDescription'])){
			$this->facilityDescription = $_POST['facilityDescription'];
		}
	}


	/**
	 * Initialize the business object with data read from the DB.
	 */
	public function initGET(){
		if (isset($_GET['facilityId'])){
			$this->facilityId = $_GET['facilityId'];
		}
		if (isset($_GET['facilityDescription'])){
			$this->facilityDescription = $_GET['facilityDescription'];
		}
	}


	/**
	 * Returns the value from the database field facilityId.
	 * In the database the field facilityId is of type int.
	 * The database field is NOT nullable.
	 * @return String the value of the field
	 */
	public function getFacilityId(){
		return $this->facilityId;
	}


	/**
	 * Set the value from the database field facilityId.
	 * In the database the field facilityId is of type int.
	 * The database field is NOT nullable.
	 * @param facilityId String with the value for the field to be set
	 */
	public function setFacilityId($facilityId){
		$this->facilityId = $facilityId;
	}


	/**
	 * Returns the value from the database field facilityDescription.
	 * In the database the field facilityDescription is of type varchar.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getFacilityDescription(){
		return $this->facilityDescription;
	}


	/**
	 * Set the value from the database field facilityDescription.
	 * In the database the field facilityDescription is of type varchar.
	 * The database field is  nullable.
	 * @param facilityDescription String with the value for the field to be set
	 */
	public function setFacilityDescription($facilityDescription){
		if(strlen($facilityDescription) > 45) throw new Exception("facilityDescription does not allow strings longer than 45 characters. The submitted string contains ".strlen($facilityDescription)." characters.");
		$this->facilityDescription = $facilityDescription;
	}
}
?>